package cn.javaxc.mvcwork.modules.file;
import cn.javaxc.mvcwork.controller.MVCConstant;
import cn.javaxc.mvcwork.controller.web.PagedListInfo;
import cn.javaxc.model.base.*;
import cn.javaxc.mvcwork.util.helper.FrameHelper;
import cn.javaxc.mvcwork.util.common.DatabaseUtils;
import java.sql.*;
import java.util.ArrayList;
import cn.javaxc.mvcwork.util.common.*;




public class FileItemVersionList extends ArrayList{

  public final static String uniqueField = "item_id";
  private java.sql.Timestamp lastAnchor = null;
  private java.sql.Timestamp nextAnchor = null;
  private int syncType = MVCConstant.NO_SYNC;

  private PagedListInfo pagedListInfo = null;
  private Object itemId = -1;
  private int owner = -1;
  private String ownerIdRange = null;
  private java.sql.Timestamp enteredRangeStart = null;
  private java.sql.Timestamp enteredRangeEnd = null;
  private int buildPortalRecords = MVCConstant.UNDEFINED;

  
  public FileItemVersionList() {
  }

 

  
  public String getUniqueField() {
    return uniqueField;
  }

  
  public void setLastAnchor(Timestamp lastAnchor) {
    this.lastAnchor = lastAnchor;
  }

  
  public void setLastAnchor(String lastAnchor) {
    this.lastAnchor = java.sql.Timestamp.valueOf(lastAnchor);
  }

  
  public void setNextAnchor(Timestamp nextAnchor) {
    this.nextAnchor = nextAnchor;
  }

  
  public void setNextAnchor(String nextAnchor) {
    this.nextAnchor = java.sql.Timestamp.valueOf(nextAnchor);
  }

  
  public void setSyncType(int syncType) {
    this.syncType = syncType;
  }

  
  public void setSyncType(String syncType) {
    this.syncType = Integer.parseInt(syncType);
  }
  


  
  public void setPagedListInfo(PagedListInfo pagedListInfo) {
    this.pagedListInfo = pagedListInfo;
  }


  
  public void setItemId(Object tmp) {
    this.itemId = tmp;
  }


  
  public Object getItemId() {
    return itemId;
  }


  
  public void setOwner(int tmp) {
    this.owner = tmp;
  }


  
  public void setOwnerIdRange(String tmp) {
    this.ownerIdRange = tmp;
  }


  
  public void setEnteredRangeStart(java.sql.Timestamp tmp) {
    this.enteredRangeStart = tmp;
  }


  
  public void setEnteredRangeEnd(java.sql.Timestamp tmp) {
    this.enteredRangeEnd = tmp;
  }


  
  public PagedListInfo getPagedListInfo() {
    return pagedListInfo;
  }


  
  public int getOwner() {
    return owner;
  }


  
  public String getOwnerIdRange() {
    return ownerIdRange;
  }


  
  public int getBuildPortalRecords() {
    return buildPortalRecords;
  }


  
  public void setBuildPortalRecords(int buildPortalRecords) {
    this.buildPortalRecords = buildPortalRecords;
  }

  
  
  public String prepareList(String sqlFilter, String sqlOrder)  {
    StringBuffer sqlSelect = new StringBuffer();


    sqlSelect.append(
        "SELECT v.* " +
            "FROM ts_files_version v " +
            "WHERE v.item_id > -1 ");
    if(sqlFilter == null || sqlFilter.length() == 0){
      StringBuffer buff = new StringBuffer();
      createFilter(buff);
      sqlFilter = buff.toString();
    }
     return (sqlSelect.toString() + sqlFilter + sqlOrder);
    
  }

  
  public void buildList()  {
    SQLMap pst = FrameHelper.getBaseSQL().createStatement();
    int items = -1;

    StringBuffer sqlFilter = new StringBuffer();
    StringBuffer sqlOrder = new StringBuffer();



    createFilter(sqlFilter);

    sqlOrder.append("ORDER BY version DESC ");

    String sql= prepareList(sqlFilter.toString(), sqlOrder.toString());
	prepareFilter(pst);
    SQLList<SQLMap> elist = pst.executeQuery(sql,"");
	for (SQLMap rs:elist ){
      FileItemVersion thisItem = new FileItemVersion(rs);
      this.add(thisItem);	
	}
  }


  
  private void createFilter(StringBuffer sqlFilter) {
    if (sqlFilter == null) {
      sqlFilter = new StringBuffer();
    }

    if (!StringUtils.isNullData(itemId)) {
      sqlFilter.append("AND item_id = ? ");
    }

    if (owner != -1) {
      sqlFilter.append("AND enteredby = ? ");
    }

    if (ownerIdRange != null) {
      sqlFilter.append("AND enteredby IN (" + ownerIdRange + ") ");
    }
    if (enteredRangeStart != null) {
      sqlFilter.append("AND entered >= ? ");
    }
    if (enteredRangeEnd != null) {
      sqlFilter.append("AND entered <= ? ");
    }
    if (buildPortalRecords != MVCConstant.UNDEFINED) {
      sqlFilter.append("AND allow_portal_access = ? ");
    }
  }


  
  protected int prepareFilter(SQLMap pst)  {
    int i = 0;
    if (!StringUtils.isNullData(itemId)) {
      pst.setObject(++i, itemId);
    }
    if (owner != -1) {
      pst.setInt(++i, owner);
    }
    if (enteredRangeStart != null) {
      pst.setTimestamp(++i, enteredRangeStart);
    }
    if (enteredRangeEnd != null) {
      pst.setTimestamp(++i, enteredRangeEnd);
    }
    if (buildPortalRecords != MVCConstant.UNDEFINED) {
      pst.setBoolean(++i, buildPortalRecords == MVCConstant.TRUE);
    }
    return i;
  }


}
